$(function () {
    mui.init();
    var req = {
        'dtid': localStorage.getItem('dtid'),
        'userid': localStorage.getItem('userid'),
        'token': localStorage.getItem('token')
    }
    mui.ready(function () {
        //加载框
        mui.showLoading("正在加载..", ".warn-list");
        Request();

        //跳转工单详情页
        mui('.repair-tab-biao').on('tap', '.mui-table', function () {
            mui.openWindow({
                url: 'Yearly-table-repair-detail.html',
                id: 'Yearly--table-repair-detail.html'
            })

            //工单id
            var gdid=$(this).children('div:first-child').children().html();
            localStorage.removeItem('gdid');  //先清除 再保存
            localStorage.setItem('gdid', gdid);
            //工单状态
            var gstatu=$(this).children('div:last-child').children().html();
            localStorage.removeItem('gstatu');  //先清除 再保存
            localStorage.setItem('gstatu', gstatu);
        })

    });

    //ajax请求处理数据
    function Request() {
        var url = domain + "/dtwbgl/dtwblist.h5";
        $.ajax(url, {
            // data: JSON.stringify(req),
						data: req,
            dataType: 'json', //服务器返回json格式数据
            type: 'post', //HTTP请求类型
            timeout: 30000, //超时时间
            headers: {
               'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            async: true, //true异步加载
            success: function (data) {
                if (data.code == 2001) {
                    //返回值处理

                    /** 按照时间升序排列 */
                    var ref = data.data;
                    console.log(ref);
                    var comref = ref.sort(compare('starttime'));
                    console.log(comref);

                    var len = comref.length;
                    var html = '';
                    for (var i = 0; i < len; i++) {
                        var thedata = comref[i];

                        var caseid = thedata.caseid; //  工单号
                        var starttime = thedata.starttime; //  计划时间
                        var zxtime = thedata.ctr_tjtime; //  执行时间
                        var cqdays = parseInt(thedata.cqdays); //超期天数
                        var wbtype = parseInt(thedata.jhtype); //维保类型
                        var theclass='';
                        // var statu = parseInt(thedata.statu);  //工单状态

                        //  维保类型判断
                        if (wbtype == 1) {
                            wbtype = '半月维保';
                        } else if (wbtype == 2) {
                            wbtype = '季度维保';
                        } else if (wbtype == 3) {
                            wbtype = '半年维保';
                        } else {
                            wbtype = '年度维保';
                        }
                        //维保状态
                        if (cqdays > 0) { //超期
                            theclass='on';
                            if (zxtime != '') {
                                cqdays = '超期' + Math.abs(cqdays) + '天完成'; //超期完成
                            } else {
                                cqdays = '超期' + Math.abs(cqdays) + '天';//超期未完成
                            }
                        } else {  //未超期
                            if (zxtime != '') {
                                cqdays = '完成';
                                theclass='';  //未超期完成
                            } else {
                                cqdays = '';
                                theclass=''; //未超期 待处理
                            }
                        }
                        //执行时间
                        if (zxtime == '') {
                            zxtime = '';  //未执行
                        }

                        html += `
                       <div class="mui-table repair-tab-body">
                            <div class="mui-table-cell mui-text-center">
                                <p>${caseid}</p>
                            </div>
                            <div class="mui-table-cell mui-text-center">
                                <p>${wbtype}</p>
                            </div>
                            <div class="mui-table-cell mui-text-center">
                                <p>${starttime}</p>
                            </div>
                            <div class="mui-table-cell mui-text-center">
                                <p>${zxtime}</p>
                            </div>
                            <div class="mui-table-cell mui-text-center">
                                <p class="${theclass}">${cqdays}</p>
                            </div>
                        </div>
                       `;
                    }
                    $('.repair-tab-biao').append(html);

                    //隐藏加载框
                    mui.hideLoading(function () { //隐藏后的回调函数  
                    });
                } else if (data.code == 2002) {
                    mui.alert(data.msg);
                    mui.openWindow({
                        url: '../../login.html',
                        id: 'login.html'
                    })
                } else {
					mui.alert(data.msg);
                   mui.openWindow({
                   		url: '404.html',
                   		id: '404.html'
                   })
                }
            },
            error: function (xhr, type, errorThrown) { //异常处理； console.log(type);
                mui.alert(type);
                mui.openWindow({
                	url: '../../login.html',
                	id: 'login.html'
                })
            }
        });
    }
    //时间升序
    function compare(property) {
        return function (a, b) {
            var value1 = a[property];
            var value2 = b[property];
            return Date.parse(value1) - Date.parse(value2);
        }
    }

})